//
//  RegistViewController.m
//  B
//
//  Created by lanou on 16/5/12.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "RegistViewController.h"
#import "NSString+QHMobile.h"
#import "RegistDetailViewController.h"

#define kGetCode @"http://app.dunkhome.com/v2/yuntongxun/send_register_code"
#define kRegist @"http://app.dunkhome.com/v2/yuntongxun/verify_sms_code"
@interface RegistViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *code;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
// 获取验证码
- (IBAction)getCode:(id)sender {
    if ([self isBlankString:_phone.text]) {
        [self.view popMessageWithTitle:@"请输入手机号码" postion:CENTER];
    } else {
        if (_phone.text.checkPhoneNumInput) { // 正则表达式
            // 获取验证码请求
            [self getCodeWithPhone];
        } else {
            if (_phone.text.length != 13) {
                [self.view popMessageWithTitle:@"手机号码不正确" postion:CENTER];
            }
        }
    }
}
- (void)getCodeWithPhone {
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session POST:kGetCode parameters:@{@"phone":self.phone.text} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger issuccess = [responseObject[@"success"] intValue];
        if (issuccess == 1) {
            [self.view popMessageWithTitle:@"发送成功" postion:CENTER];
        } else {
            [self.view popMessageWithTitle:responseObject[@"message"] postion:CENTER];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"--error%@", error);
    }];
}
- (IBAction)next:(id)sender { // 下一步
    // 点击下一步先请求注册接口
    [[AFHTTPSessionManager manager] POST:kRegist parameters:@{@"code":self.code.text, @"phone":self.phone.text} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger result = [responseObject[@"success"] intValue];
        // 如果请求成功则push到下一页
        if (result == 1) {
            RegistDetailViewController *registDetail = [RegistDetailViewController new];
            registDetail.phone = _phone.text;
            registDetail.password = _password.text;
            [self.navigationController pushViewController:registDetail animated:YES];
        } else {
            [self.view popMessageWithTitle:responseObject[@"message"] postion:CENTER];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
